function PageBackendSample(user, sd, options) {
  this._source = user;
  this._sd = sd;
  this._options = (options) ? options : null;
  
  this._gui = { name: document.getElementById('input1'),
                type: document.getElementById('input2'),
                model: document.getElementById('input3'),
                price: document.getElementById('input4'),
                naviStatus: document.getElementById('td1'),
                attributes: document.getElementById('td2'),
                navigation: document.getElementById('img1'),
                btnBack: document.getElementById('img2'),
                attributeNew: document.getElementById('img3'),
                btnSave: document.getElementById('img4')
              };
  
  this._gui.type.options[this._gui.type.options.length] = new Option('');
  for (var i = 0, il = SampleType.array().length; i < il; i++) {
    var type = SampleType.array()[i];
    option = new Option(type);
    this._gui.type.options[this._gui.type.options.length] = option;
  }
  this._gui.model.options[this._gui.model.options.length] = new Option('');
  for (var i = 0, il = Manage.array().length; i < il; i++) {
    var model = Manage.array()[i];
    if (model.type != '{{$smarty.const.MODEL_TYPE_PRODUCT|escape:javascript}}') {
      continue;
    }
    option = new Option(model.title);
    option._model = model;
    this._gui.model.options[this._gui.model.options.length] = option;
  }
  
  this._createElements();
};

PageBackendSample.prototype._createElements = function() {
  var a, tr, td, _self = this;
  
  this._gui.navigation.onclick = function() { location.href = '?p=pageasst&t=pagebackendsamples&m1=a4&m2=b2'; };
  DOMUtils.removeChildElements(this._gui.naviStatus);
  var img = document.createElement('img');
  if (this._options.sid) {
    img.src = 'images/pageasst/pagebackendsample/editproduct.png';
  } else {
    img.src = 'images/pageasst/pagebackendsample/addnewproduct.png';
  }
  this._gui.naviStatus.appendChild(img);
  
  this._loadData();
};

PageBackendSample.prototype._loadData = function() {
  this._sample = null;
  this._retrieveSample();
};

PageBackendSample.prototype._verifyData = function() {
  if (this._sample) {
    this._updateElements();
  }
};

PageBackendSample.prototype._retrieveSample = function() {
  var _self = this;
  if (this._options.sid) {
    new RequestUtils()._read('sample', null, 'd.oid = ' + this._options.sid, null, null, null, null, function(result, params) { _self._sample = (result.data.length == 1) ? result.data[0] : null;
                                                                                                                                _self._verifyData.call(_self);
                                                                                                                              }, null);
  } else {
    this._sample = Sample.instance();
    this._verifyData();
  }
};

PageBackendSample.prototype._updateElements = function() {
  var table, tr, td, _self = this;

  this._gui.name.value = this._sample.name;
  this._gui.price.value = this._sample.price;
  for (var i = 0, il = this._gui.type.options.length; i < il; i++) {
    var option = this._gui.type.options[i];
    if (option.text == this._sample.type) {
      this._gui.type.selectedIndex = i;
    }
  }
  for (var i = 0, il = this._gui.model.options.length; i < il; i++) {
    var option = this._gui.model.options[i];
    if (option._model && option._model.name == this._sample.model) {
      this._gui.model.selectedIndex = i;
    }
  }
  
  this._gui.name.onchange = function() { _self._sample.name = this.value; };
  this._gui.type.onchange = function() { _self._sample.type = this.options[this.selectedIndex].text; 
                                         _self._sample.attributes = DlmanageUtils.attributes(_self._sample.type);
                                         _self._updateElements.call(_self);
                                       };
  this._gui.model.onchange = function() { _self._sample.model = (this.options[this.selectedIndex]._model) ? this.options[this.selectedIndex]._model.name : ''; };
  this._gui.price.onchange = function() { _self._sample.price = this.value; };
  
  this._gui.attributeNew.onclick = function() { _self._sample.attributes[_self._sample.attributes.length] = SampleAttribute.instance();
                                                _self._updateElements.call(_self);
                                                return false;
                                              };
  
  DOMUtils.removeChildElements(this._gui.attributes);
  table = document.createElement('table');
  this._gui.attributes.appendChild(table);
  for (var i = 0, il = this._sample.attributes.length; i < il; i++) {
    var attribute = this._sample.attributes[i];
    tr = table.insertRow(-1);
    td = tr.insertCell(-1);
    new EditSampleAttribute(td, attribute, function() { _self._updateElements.call(_self); }, { prefix: new Date().getTime() + '_', index: i, parent: this._sample });
  }
  
  this._gui.btnSave.onclick = function() { new RequestUtils()._write('sample', [_self._sample], [], function() { location.href = '?p=pageasst&t=pagebackendsamples&m1=a4&m2=b2'; }, null); };
  this._gui.btnBack.onclick = function() { location.href = '?p=pageasst&t=pagebackendsamples&m1=a4&m2=b2'; };
};

